package org.wenhuayuan.library.log;

import androidx.annotation.NonNull;

import java.util.Arrays;
import java.util.List;

/**
 * Tips:
 * 1，打印堆栈信息
 * 2，file输出
 * 3，模拟控制台
 */
public class HiLog {
    private static String LOG_PACKAGE;
    static {
        String clsName = HiLog.class.getName();
        LOG_PACKAGE = clsName.substring(0, clsName.lastIndexOf(".") + 1);
    }
    public static void v(Object... contents) {
        log(HiLogType.V, contents);
    }
    public static void vt(String tag, Object... contents) {
        log(HiLogType.V, tag, contents);
    }

    public static void d(Object... contents) {
        log(HiLogType.D, contents);
    }

    public static void dt(String tag, Object... contents) {
        log(HiLogType.D, tag, contents);
    }

    public static void i(Object... contents) {
        log(HiLogType.I, contents);
    }

    public static void it(String tag, Object... contents) {
        log(HiLogType.I, tag, contents);
    }
    public static void w(Object... contents) {
        log(HiLogType.W, contents);
    }

    public static void wt(String tag, Object... contents) {
        log(HiLogType.W, tag, contents);
    }
    public static void e(Object... contents) {
        log(HiLogType.E, contents);
    }

    public static void et(String tag, Object... contents) {
        log(HiLogType.E, tag, contents);
    }
    public static void a(Object... contents) {
        log(HiLogType.A, contents);
    }

    public static void at(String tag, Object... contents) {
        log(HiLogType.A, tag, contents);
    }

    private static void log(@HiLogType.TYPE int type, Object... contents) {
        log(type, HiLogManager.getInstance().getHiLogConfig().getGlobalTag(), contents);
    }
    private static void log(@HiLogType.TYPE int type, @NonNull String tag, Object... contents) {
        log(HiLogManager.getInstance().getHiLogConfig(), type, tag, contents);
    }
    public static void log(@NonNull HiLogConfig hiLogConfig, @HiLogType.TYPE int type, @NonNull String tag, Object... contents) {
        if (!hiLogConfig.enable()) {
            return;
        }

        StringBuilder sb = new StringBuilder();
        if(hiLogConfig.includeThread()) {
            String threadInfo = HiLogConfig.HI_THREAD_FORMATTER.format(Thread.currentThread());
            sb.append(threadInfo).append("\n");
        }
        if(hiLogConfig.stackTraceDepth() > 0) {
            String stackTrace = HiLogConfig.HI_STACK_TRANCE_FORMATTER.format(HiStackTraceUtil.getCropRealStackTrace(new Throwable().getStackTrace(), LOG_PACKAGE, hiLogConfig.stackTraceDepth()));
            sb.append(stackTrace).append("\n");
        }
        String body = parseBody(contents, hiLogConfig);
        sb.append(body);
        List<HiLogPrinter> printers = hiLogConfig.printers() != null ? Arrays.asList(hiLogConfig.printers()) : HiLogManager.getInstance().getPrinters();
        if (null == printers) {
            return ;
        }
        // 打印log
        for (HiLogPrinter printer: printers) {
            printer.print(hiLogConfig, type, tag, sb.toString());
        }
    }

    private static String parseBody(@NonNull Object[] contents, HiLogConfig config) {
        if (null != config.injectJsonParser()) {
           return config.injectJsonParser().toJson(contents);
        }
        StringBuilder sb = new StringBuilder();
        for (Object content:contents) {
            sb.append(content.toString()).append(";");
        }

        if(sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }


}
